/*
** Licensed under the Apache License, Version 2.0 (the "License");
** you may not use this file except in compliance with the License.
** You may obtain a copy of the License at
**
**     http://www.apache.org/licenses/LICENSE-2.0
**
** Unless required by applicable law or agreed to in writing, software
** distributed under the License is distributed on an "AS IS" BASIS,
** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
** See the License for the specific language governing permissions and
** limitations under the License.
*/

package com.duoqin.common;

import android.app.IEventRecordService;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.IBinder;
import android.os.RemoteException;
import android.util.Log;

/**
* System private event record manager.
* {@hide}
*/
public class EventRecordManager {
    private static final String TAG = "ERService";
    private static final Object mServiceLock = new Object();
    private static IEventRecordService mService = null;
    private static EventRecordManager mInstance;

    private static final ComponentName DEFAULT_COMPONENT = new ComponentName(
        "com.duoqin.ers", "com.duoqin.ers.ERService");
    private static final RecordConnection mRecordConn = new RecordConnection();

    static class RecordConnection implements ServiceConnection {
        public void onServiceConnected(ComponentName name, IBinder service) {
            Log.d(TAG, "onServiceConnected");

            if (service == null) {
                Log.i(TAG, "service == null");
                return;
            }

            if (mService != null) {
                synchronized (mServiceLock) {
                    mService = IEventRecordService.Stub.asInterface(service);
                }
            }
            else {
                mService = IEventRecordService.Stub.asInterface(service);
            }
        }

        public void onServiceDisconnected(ComponentName name) {
            Log.d(TAG, "onServiceDisconnected");
            if (mService != null) {
                synchronized (mServiceLock) {
                    mService = null;
                }
            }
        }
    }

    public static EventRecordManager getInstance(Context context) {
        if (mInstance == null) {
            mInstance = new EventRecordManager(context.getApplicationContext());
        }
        return mInstance;
    }

    private EventRecordManager(Context context) {
        if (mService == null) {
            connectToService(context);
        }
    }


    private boolean connectToService(Context context) {
        Log.i(TAG, "Trying to bind to ERService");

        try {
            Intent service = new Intent().setComponent(DEFAULT_COMPONENT);
            if (context.bindService(service, mRecordConn, Context.BIND_AUTO_CREATE)) {
                return true;
            }
        } catch (Exception e) {
            Log.e(TAG, "failed to bind to ERService");
        }
        return false;
    }

    public void onEvent(String event_id, String label) {
        if (mService != null) {
            synchronized (mServiceLock) {
                try {
                    mService.onEvent(event_id, label);
                } catch (RemoteException e) {
                    Log.e(TAG, "event RemoteException");
                    mService = null;
                }
            }
        }
    }

    public void onEventStart(String event_id, String label) {
        if (mService != null) {
            synchronized (mServiceLock) {
                try {
                    mService.onEventStart(event_id, label);
                } catch (RemoteException e) {
                    Log.e(TAG, "event RemoteException");
                    mService = null;
                }
            }
        }
    }

    public void onEventEnd(String event_id, String label) {
        if (mService != null) {
            synchronized (mServiceLock) {
                try {
                    mService.onEventEnd(event_id, label);
                } catch (RemoteException e) {
                    Log.e(TAG, "event RemoteException");
                    mService = null;
                }
            }
        }
    }

    public void onEventDuration(String event_id, String label, long milliseconds) {
        if (mService != null) {
            synchronized (mServiceLock) {
                try {
                    mService.onEventDuration(event_id, label, milliseconds);
                } catch (RemoteException e) {
                    Log.e(TAG, "event RemoteException");
                    mService = null;
                }
            }
        }
    }
}
